﻿using BLL;
using Common.Class;
using Common.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace App_Start
{

    /// <summary>
    /// 判断是否已登录
    /// </summary>
    public class CheckLoginFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {

            try
            {
                string _paramStr = Convert.ToString(context.HttpContext.Request.Form["ParamStr"]);
                string _tokenid = Convert.ToString(context.HttpContext.Request.Form["TokenID"]);
                JsonHelper _jsonParam = new JsonHelper(_paramStr);
                if (string.IsNullOrWhiteSpace(_tokenid))
                {
                    context.Result = new ContentResult()
                    {
                        StatusCode = 200,
                        Content = JsonHelper.GetJsonStrFromObject(new ServiceResultModel { Success = -999 })
                    };
                    return;
                }
                if (string.IsNullOrWhiteSpace(Convert.ToString(CacheHelper.CacheGet("TokenID_" + _tokenid))))
                {
                    context.Result = new ContentResult()
                    {
                        StatusCode = 200,
                        Content = JsonHelper.GetJsonStrFromObject(new ServiceResultModel { Success = -999 })
                    };
                    return;
                }

            }
            catch (Exception ex)
            {
                context.Result = new ContentResult()
                {
                    StatusCode = 500,
                    Content = "请求错误"
                };
                string _errorID = Convert.ToString(CommonHelper.GetNewID(1));
                LogHelper.WriteLogToFile("Error", _errorID + "---" + ex.Message + "\r\n" + ex.StackTrace, "");
                return;
            }

        }
    }

    /// <summary>
    /// 判断是否为本地请求
    /// </summary>
    public class CheckLocalFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {

            try
            {
                if (ConfigBLL.Config.IsMastLocal && context.HttpContext.Request.Host.ToString().IndexOf("127.0.0.1") < 0)
                {

                    context.Result = new ContentResult()
                    {
                        StatusCode = 500,
                        Content = "未授权的请求"
                    };
                    return;
                }
            }
            catch (Exception ex)
            {
                context.Result = new ContentResult()
                {
                    StatusCode = 500,
                    Content = "请求错误"
                };
                string _errorID = Convert.ToString(CommonHelper.GetNewID(1));
                LogHelper.WriteLogToFile("Error", _errorID + "---" + ex.Message + "\r\n" + ex.StackTrace, "");
                return;
            }

        }
    }

}
